<?php
/*
    Copyright (C) 2008 - 2010 Inanta Martsanto
    
    This file is part of Koma.

    Koma is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Koma is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Koma.  If not, see <http://www.gnu.org/licenses/>.
    
    Inanta Martsanto (inanta@inationsoft.com)
*/

include('mainfile.php');

define('LOGOUT', 'logout');
define('LOGIN', 'login');

$komaAuthentication = KomaAuthentication::getInstance();

switch($_GET['action'])
{
    case LOGIN:
        if($komaAuthentication->login($_POST['uname'], $_POST['pass']))
        {
            $komaSession->regenerateID();
            $komaCookie = KomaCookie::getInstance();

            if($_POST['remember'])
            {
                $komaCookie->set('uname', $_POST['uname']);
                $komaCookie->set('remember', true);
            }
            else
            {
                $komaCookie->delete('uname');
                $komaCookie->delete('remember');
            }
            
            redirect($_POST['redirect'], sprintf(T_('Thank you for logging in, %s'), $_POST['uname']), 3);
        }
        else
        {
            redirect(KOMA_URL, T_('Incorrect Login!', 3));
        }
        
        break;
    case LOGOUT:
        if($komaAuthentication->logout($_SESSION['Token'], $_GET['token']))
        {
            $komaSession->regenerateID();
            redirect(KOMA_URL, T_('You are now logging out, thank you for visit our site'), 3);
            break;
        }
        //else
        //{
        //    redirect(KOMA_URL, T_('Illegal Action!'), 3);
        //}
        
        //break;
    default:
        redirect(KOMA_URL, T_('Illegal Action!'), 3);
        break;
}
?>